

//const serverHost = 'http://localhost:8088'
const serverHost = 'http://192.168.1.95:8088'
var coinHost = "http://coin.3wtool.com:8090/jsonrpc/"

const request = function(url, method, data, success) {
	uni.request({
		url: serverHost+url,
		method: method,
		data: data,
		success: (res) => {
			console.log("network success:" + JSON.stringify(res));
			if (res.data.data != null) {
				success(res.data.data);
			} else {
				uni.showToast({
					title: '错误码:' + res.data.error.code + ', 错误信息:' + res.data.error.message,
					icon: 'none'
				});
			}
		},
		fail: (res) => {
			console.log("network failure:" + JSON.stringify(res));
			uni.showToast({
				title: '网络错误!',
				duration: 1500
			});
		}
	});
}


const requestWithHeader = function(url, method, header, data, success) {
	uni.request({
		url: serverHost+url,
		method: method,
		header: header,
		data: data,
		success: (res) => {
			console.log("network success:" + JSON.stringify(res));
			if (res.data.data != null) {
				success(res.data.data);
			} else {
				uni.showToast({
					title: '错误码:' + res.data.error.code + ', 错误信息:' + res.data.error.message,
					icon: 'none'
				});
			}
		},
		fail: (res) => {
			console.log("network failure:" + JSON.stringify(res));
			uni.showToast({
				title: '网络错误!',
				duration: 1500
			});
		}
	});
}

const requestJsonRpc = function(url, method, data, success) {
	uni.request({
		url: coinHost,
		method: method,
		data: data,
		success: (res) => {
			console.log("network success:" + JSON.stringify(res));
			if (res.data.result != null) {
				success(res.data.result)
			} else if (res.data.error != null) {
			  uni.showToast({
					title: '错误信息:' + res.data.error.message,
					icon: 'none'
			  });
			}
		},
		fail: (res) => {
			console.log("network failure:" + JSON.stringify(res));
			uni.showToast({
				title: '网络错误!',
				duration: 1500
			});
		}
	});
}

export default {
	request,
	requestWithHeader,
	requestJsonRpc
}

